﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TSComm.Utilities.Datas;

namespace JSTT_DATA_ENTITY.XMLDatas
{
    public class DataMatchItem
    {
        private string _name;
        private string _dataType;
        private int _byteStart;
        private int _byteLength;
        private int _bitStart;
        private int _bitLength;
        private string _itemDataUnit;
        private string _itemMatchVal;

        public DataMatchItem()
        {
            
        }

        public DataMatchItem(string name, string itemMatchVal, string dataType, int byteStart, int byteLength,
            int bitStart = 0, int bitLength = 0, string itemDataUnit = "byte")
        {
            _name = name;
            _itemMatchVal = itemMatchVal;
            _dataType = dataType;
            _byteStart = byteStart;
            _byteLength = byteLength;
            _bitStart = bitStart;
            _bitLength = bitLength;
            _itemDataUnit = itemDataUnit;
        }

        [XmlAttribute("ItemName")]
        public string ItemName
        {
            get { return _name; }
            set { _name = value; }
        }

        [XmlAttribute("ItemMatchVal")]
        public string ItemMatchVal
        {
            get { return _itemMatchVal; }
            set { _itemMatchVal = value; }
        }

        [XmlAttribute("ItemDataType")]
        public string ItemDataType
        {
            get { return _dataType; }
            set { _dataType = value; }
        }

        [XmlAttribute("ByteStart")]
        public int ByteStart
        {
            get { return _byteStart; }
            set { _byteStart = value; }
        }

        [XmlAttribute("ByteLength")]
        public int ByteLength
        {
            get { return _byteLength; }
            set { _byteLength = value; }
        }

        [XmlAttribute("BitStart")]
        public int BitStart
        {
            get { return _bitStart; }
            set { _bitStart = value; }
        }

        [XmlAttribute("BitLength")]
        public int BitLength
        {
            get { return _bitLength; }
            set { _bitLength = value; }
        }

        [XmlAttribute("ItemDataUnit")]
        public string ItemDataUnit
        {
            get { return _itemDataUnit; }
            set { _itemDataUnit = value; }
        }
    }


    [XmlRoot("DataRuleMatch")]
    public class JSTT_DataRuleMatchs 
    {
        private int _jstt_dataRulematchLength=0;
        private List<DataMatchItem> dtMatchItems = new List<DataMatchItem>();

        [XmlElement("JSTT_DataRulematchLength")]
        public int JSTT_DataRulematchLength
        {
            get { return _jstt_dataRulematchLength; }
            set { _jstt_dataRulematchLength = value; }
        }

        [XmlArrayItem("DataMatchItem", typeof(DataMatchItem))]
        [XmlArray("JSTT_DataRuleMatchItems")]
        public DataMatchItem[] JSTT_DataRuleMatchItems
        {
            get
            {
                return dtMatchItems.ToArray();
            }
            set
            {
                foreach (DataMatchItem dataMatchitem in value)
                {
                    dtMatchItems.Add(dataMatchitem);
                }
            }
        }


        public void AddDataMatch(DataMatchItem dmi)
        {
            dtMatchItems.Add(dmi);
        }

        public bool ToMatchData(byte[] datas)
        {
            // 长度判断
            if (_jstt_dataRulematchLength > 0 && datas.Length!=_jstt_dataRulematchLength)
            {
                return false;
            }

            foreach (DataMatchItem dmi in this.dtMatchItems)
            {
                byte[] dstDatas = datas.CopyBytes(dmi.ByteStart, dmi.ByteLength);
                if (dstDatas != null)
                {
                    if (dmi.ItemDataType == "Number")
                    {
                        //int dstVal = dstDatas.ToIntH(0);
                        //int dstVal = (int)ByteHelper.ConvertByteData(dstDatas, "int", dmi.ByteLength);
                        var ii = ByteHelper.ConvertByteData(dstDatas, "int", dmi.ByteLength);
                        int dstVal = Convert.ToInt32(ii); 
                        int enforceVal = 0;
                        if (dmi.ItemMatchVal[dmi.ItemMatchVal.Length - 1] == 'H')
                        {
                            string _dataMatchVal1 = dmi.ItemMatchVal.Substring(0, dmi.ItemMatchVal.Length - 1);
                            enforceVal = Convert.ToInt32(_dataMatchVal1, 16);
                        }
                        else
                        {
                            enforceVal = Convert.ToInt32(dmi.ItemMatchVal);
                        }
                        if (dstVal != enforceVal)
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }

            return true;
        }
    }
}
